package org.kailu.book.controller;


import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.kailu.book.entity.BillBookDetailsEntity;
import org.kailu.book.entity.BillEntity;
import org.kailu.book.entity.BillStationeryDetailsEntity;
import org.kailu.book.entity.BookEntity;
import org.kailu.book.entity.CustomerEntity;
import org.kailu.book.entity.StationeryEntity;
import org.kailu.book.service.BillService;
import org.kailu.book.service.BookDetailsService;
import org.kailu.book.service.BookService;
import org.kailu.book.service.CustomerService;
import org.kailu.book.service.StationeryDetailsService;
import org.kailu.book.service.StationeryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

/**
 * @author viet.ch
 * @generated May 25, 2014
 * @from Ho Chi Minh, VN
 */

@Controller
public class BillController {

	@Autowired
	private BillService billService;
	@Autowired
	private BookService	 bookService;
	@Autowired
	private StationeryService stationService;
	@Autowired
	private CustomerService customerService;
	@Autowired
	private BookDetailsService bdService;
	@Autowired
	private StationeryDetailsService sdService;
	
	@RequestMapping("/bill")
	public String getData(ModelMap map) {
		map.addAttribute("billList", billService.getAll());
		return "bill";
	}
	
	@RequestMapping("/bill/new")
	public String newBill(ModelMap map) {
		map.addAttribute("bookList", bookService.getAll());
		map.addAttribute("stationeryList", stationService.getAll());
		map.addAttribute("customerList", customerService.getAll());
		map.addAttribute("bill", new BillEntity());
		map.addAttribute("book_details", new BillBookDetailsEntity());
		map.addAttribute("stationery_details", new BillStationeryDetailsEntity());
		return "bill_new";
	}
	
	@RequestMapping(value = "/bill/saveorupdate", method = RequestMethod.POST)
    public String save(HttpServletRequest request)
    {
		CustomerEntity customer = customerService.get(Integer.parseInt((request.getParameter("customerId").toString())));
		Date date = new Date();
		int isExport = 0;
		BillEntity bill = new BillEntity(customer,date);
		
		billService.saveOrUpdate(bill);
		
		//insert book
		for (int i = 1; i < 20; i++) {
			
			if ( request.getParameter("bookId" + i)  ==  null) {
				continue;
			} else {
				int bookId = Integer.parseInt(request.getParameter("bookId" + i));
				int bookQuantity = Integer.parseInt(request.getParameter("bookQuantity" + i));
				
				BookEntity bookEn = bookService.get(bookId);
				bdService.saveOrUpdate(new BillBookDetailsEntity(bookEn,bill,bookQuantity));
			}
		}
		
		//insert stationery
		for (int i = 1; i < 20; i++) {
			
			String stationeryIdName = "stationeryId" + i;
			
			if (request.getParameter("stationeryId" + i) == null) {
				
				System.out.println("breake: " + stationeryIdName);
				
				continue;
			} else {
				int stationeryId = Integer.parseInt(request
						.getParameter("stationeryId" + i));
				int stationeryQuantity = Integer.parseInt(request
						.getParameter("stationeryQuantity" + i));
				StationeryEntity stationeryEn = stationService.get(stationeryId);
				sdService.saveOrUpdate(new BillStationeryDetailsEntity(stationeryEn, bill,
						stationeryQuantity));
			}
		}
		
		//123
		
		long money = updateDatabase(bill.getBillId(), isExport);
		
		bill.setBillMoney(money);
		
		billService.saveOrUpdate(bill);
		
		return "redirect:/invoice/" + bill.getBillId();
    }
	
	public long updateDatabase(int billId, int isExport) {

		//get data
		List<BillBookDetailsEntity> bookList = bdService.getByBillId(billId);
		List<BillStationeryDetailsEntity> StationeryList = sdService.getByBillId(billId);
		
		long money = 0;
		
		// update for stationery
		for (BillStationeryDetailsEntity billStationeryDetailsEntity : StationeryList) {
			StationeryEntity se = stationService
					.get(billStationeryDetailsEntity.getStationery()
							.getStationeryId());
			if (isExport == 1) {
				se.setStationeryQuantity(se.getStationeryQuantity()
						- billStationeryDetailsEntity.getQuantity());
			} else {
				se.setStationeryQuantity(se.getStationeryQuantity()
						+ billStationeryDetailsEntity.getQuantity());
			}
			
			money = money + se.getStationeryPrice() *billStationeryDetailsEntity.getQuantity();
			
			stationService.saveOrUpdate(se);
		}
		
		//update for book
		for (BillBookDetailsEntity billBookDetailsEntity : bookList) {
			BookEntity se = bookService.get(billBookDetailsEntity.getBook()
					.getBookId());
			if (isExport == 1) {
				se.setBookQuantity(se.getBookQuantity()
						- billBookDetailsEntity.getQuantity());
			} else {
				se.setBookQuantity(se.getBookQuantity()
						+ billBookDetailsEntity.getQuantity());
			}
			
			money = money + se.getBookPrice() * billBookDetailsEntity.getQuantity();
			
			bookService.saveOrUpdate(se);
		}
		
		return money;
	}
	
	@RequestMapping(value = "/invoice/{billId}", method= RequestMethod.GET)
	public String fillReport(@PathVariable("billId") int billId, ModelMap map) {
		
		List<BillBookDetailsEntity> bookList = bdService.getByBillId(billId);
		List<BillStationeryDetailsEntity> StationeryList = sdService.getByBillId(billId);
		
		map.addAttribute("bill", billService.get(billId));
		map.addAttribute("bookList",bookList);
		map.addAttribute("stationeryList", StationeryList);
		map.addAttribute("customer", billService.get(billId).getCustomer());
		return "invoice";
	}
	
	@RequestMapping(value = "/backlog/stationery", method= RequestMethod.GET)
	public String backlog_s(ModelMap map) {
		map.addAttribute("stationeryList", stationService.getAll());
		return "backlog";
	}
	
	@RequestMapping(value = "/backlog/book", method= RequestMethod.GET)
	public String backlog_b(ModelMap map) {
		map.addAttribute("bookList", bookService.getAll());
		return "backlog";
	}
	
	@RequestMapping(value = "/backlog", method= RequestMethod.GET)
	public String backlog(ModelMap map) {
		map.addAttribute("bookList", bookService.getAll());
		map.addAttribute("stationeryList", stationService.getAll());
		return "backlog";
	}
	
	@RequestMapping(value = "/bill", method = RequestMethod.POST)
    public String search(HttpServletRequest request, ModelMap map)
    {
		String from_date = request.getParameter("from_date");
		String to_date = request.getParameter("to_date");
		map.addAttribute("billList", billService.search(from_date, to_date));
		return "bill";
    }
	
	@RequestMapping("/")
	public String dashboard(){
		return "doashboard";
	}
	
}
